﻿using log4net.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace log4net
{
    public class LogService 
    {
        static LogService()
        {
            Init();
        }
        static ILog log;
        public static void Init()
        {
            DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
            var configFile = dir.FullName + "config\\log4net.config";
            XmlConfigurator.Configure(new FileInfo(configFile));
            log = LogManager.GetLogger("sysLog");
        }
        public void AddLog(LogLevel level, string msg)
        {
            WriteLog(level, msg: msg);
        }

        public void AddLog(LogLevel level, Exception ex)
        {
            WriteLog(level, ex: ex);
        }

        public void AddLog(LogLevel level, string msg, Exception ex)
        {
            WriteLog(level, msg, ex);
        }

        static void WriteLog(LogLevel level, string msg = "", Exception ex = null)
        {
            if (log == null)
            {
                return;
            }

            switch (level)
            {
                case LogLevel.DeBug:
                    log.Debug(msg, ex);
                    break;
                case LogLevel.Info:
                    log.Info(msg, ex);
                    break;
                case LogLevel.Warning:
                    log.Warn(msg, ex);
                    break;
                case LogLevel.Exception:
                    log.Error(msg, ex);
                    break;
                default:
                    break;
            }
        }
    }
}
